package com.weimob.mcloud.wechatwork.customer.service.impl;

import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.Tag;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.TagDetail;
import com.weimob.mcloud.wechatwork.core.entity.relation.tag.TagGroup;
import com.weimob.mcloud.wechatwork.customer.convertor.tag.CustomerTagCovert;
import com.weimob.mcloud.wechatwork.customer.service.CustomerEmployeeTagService;
import com.weimob.mcloud.wechatwork.customer.service.EmployeeCustomerTagPressureTestService;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BizException;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.tag.EmployeeCustomerTagPressureTestBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.tag.TagBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.tag.TagGroupBO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.req.tag.EmployeeCustomerTagPressureTestDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class EmployeeCustomerTagPressureTestServiceImpl implements EmployeeCustomerTagPressureTestService {

    @Autowired
    private CustomerEmployeeTagService customerEmployeeTagService;

    @Value("${query.employee.customer.tag.needFullInfo:true}")
    private Boolean queryEmployeeCustomerTagNeedFullInfo;

    @Override
    public EmployeeCustomerTagPressureTestBO fetchCustomerEmployeeTag(EmployeeCustomerTagPressureTestDTO request) {
        if (null == request || StringUtils.isBlank(request.getCorpId())
                || StringUtils.isBlank(request.getExternalUserId())
                || StringUtils.isBlank(request.getOrgUserId())) {

            throw new BizException("参数不合法");
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(request.getCorpId());
        privateSphere.setBosId(request.getBosId());

        List<TagDetail> tagDetailList = customerEmployeeTagService.fetchCustomerEmployeeTag(privateSphere,
                request.getExternalUserId(), request.getOrgUserId(), queryEmployeeCustomerTagNeedFullInfo);
        if (CollectionUtils.isEmpty(tagDetailList)) {
            return null;
        }

        List<TagBO> tagBOList = new ArrayList<>();
        for (TagDetail tagDetail : tagDetailList) {

            Tag tag = tagDetail.getTag();
            if (null == tag) {
                continue;
            }

            TagBO tagBO = new TagBO();
            tagBO.setExternalTagId(tag.getExternalTagId());
            tagBO.setTagName(tag.getTagName());

            TagGroup tagGroup = tag.getTagGroup();
            if (null != tagGroup) {
                TagGroupBO tagGroupBO = new TagGroupBO();
                tagGroupBO.setExternalTagGroupId(tagGroup.getExternalTagGroupId());
                tagGroupBO.setTagGroupName(tagGroup.getTagGroupName());
                tagBO.setTagGroupBO(tagGroupBO);
            }

            tagBOList.add(tagBO);
        }

        EmployeeCustomerTagPressureTestBO result = new EmployeeCustomerTagPressureTestBO();
        result.setCorpId(request.getCorpId());
        result.setExternalUserId(request.getExternalUserId());
        result.setOrgUserId(request.getOrgUserId());
        result.setTagBOList(tagBOList);

        return result;
    }

    @Override
    public Boolean markTag(EmployeeCustomerTagPressureTestDTO request) {

        if (null == request || StringUtils.isBlank(request.getCorpId())
                || StringUtils.isBlank(request.getExternalUserId())
                || StringUtils.isBlank(request.getOrgUserId())) {
            throw new BizException("参数不合法");
        }

        if (CollectionUtils.isEmpty(request.getAddTagIdList()) && CollectionUtils.isEmpty(request.getRemoveTagIdList())) {
            throw new BizException("参数不合法");
        }

        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(request.getCorpId());
        privateSphere.setBosId(request.getBosId());

        List<TagDetail> tagDetailList = customerEmployeeTagService.fetchCustomerEmployeeTag(privateSphere,
                request.getExternalUserId(), request.getOrgUserId(), false);

        List<String> dbTagIdList = CustomerTagCovert.toTagIds(tagDetailList);

        List<String> analysisTagIdList = analysisTagIdList(dbTagIdList, request.getAddTagIdList(), request.getRemoveTagIdList());
        if (CollectionUtils.isNotEmpty(analysisTagIdList)) {
            analysisTagIdList = analysisTagIdList.stream().distinct().collect(Collectors.toList());
        }

        if (CollectionUtils.isEmpty(tagDetailList)) {
            return customerEmployeeTagService.saveCustomerEmployeeTag(privateSphere, request.getExternalUserId(), request.getOrgUserId(), analysisTagIdList);
        } else {
            return customerEmployeeTagService.updateCustomerEmployeeTag(privateSphere, request.getExternalUserId(), request.getOrgUserId(), analysisTagIdList);
        }
    }

    private List<String> analysisTagIdList(List<String> dbTagIdList, List<String> addTagIdList, List<String> removeTagIdList) {
        if (CollectionUtils.isEmpty(dbTagIdList)) {
            return addTagIdList;
        }

        if (CollectionUtils.isNotEmpty(addTagIdList)) {
            dbTagIdList.addAll(addTagIdList);
        }

        if (CollectionUtils.isEmpty(removeTagIdList)) {
            return dbTagIdList;
        } else {
            List<String> result = new ArrayList<>();
            for (String tagId : dbTagIdList) {
                if (!removeTagIdList.contains(tagId)) {
                    result.add(tagId);
                }
            }

            return result;
        }
    }
}
